package com.example.administrator.capital.utils;
import android.content.Context;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
 * Created by Administrator on 2017/12/5 0005.
 *
 */

public class CommonUtils {
    Context context;
    private final static Pattern emailer = Pattern
            .compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
    /**
     * 由数字和字母组成6-12位
     */
    private final static Pattern user_name = Pattern
            .compile("^[0-9A-Za-z]{1,12}$");
    /**
     * 验证手机号码
     */
    public static boolean isMobileNO(String mobiles) {
        String regExp = "^^[1]([3][0-9]{1}|59|58|88|89|87|76|56|86|85|45|83|80|81|77|82|84|50|51|52|53|55|57|47|78)[0-9]{8}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }
    public static String settingphone(String phone) {
        String phone_s = phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
        return phone_s;
    }
    /**
     * 字符串非空验证
     *
     * @param temp
     * @return
     */
    public static boolean isNull(String temp) {
        if (temp == null || temp.length() <= 0) {
            return false;
        } else {
            return true;
        }
    }
    /**
     * 验证邮箱
     *
     * @param email
     * @return
     */
    public static boolean isEmail(String email) {
        if (email == null || email.trim().length() == 0)
            return false;
        return emailer.matcher(email).matches();
    }
    /**
     * 验证用户名只能由数字字母组成(6-12)
     *
     * @param str
     * @return
     */
    public static boolean isCorrectFormat(String str) {
        if (str == null || str.trim().length() == 0)
            return false;
        return user_name.matcher(str).matches();
    }
    /**
     * 验证支付密码和验证码为六位数字
     *
     * @param mobiles
     * @return
     */
    public static boolean isPayPwdNO(String mobiles) {
        String regExp = "^\\d{6}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }
    /**
     * 转换时间格式：年月日 时分秒
     *
     * @param s 格式："yyyy-MM-dd HH:mm:ss"
     * @return
     */
    public static String getTime(String s) {
        try {
            SimpleDateFormat format = new SimpleDateFormat(
                    "yyyy-MM-dd HH:mm:ss");
            Date DateTimes = format.parse(s);
            long getTimes = DateTimes.getTime();
            Calendar cal = Calendar.getInstance();
            cal.setTime(new Date());
            long setTimes = getTimes - cal.getTimeInMillis();
            if (setTimes > 0) {
                long time_m, time_s, time_h, time_d;
                time_d = setTimes / 86400000;
                time_h = (setTimes % 86400000) / 3600000;
                time_m = ((setTimes % 3600000) / 60000);
                time_s = (setTimes / 1000) % 60;
                if (time_s < 10) {
                    return (time_d + "天:" + time_h + "小时:" + time_m + "分钟:" + "0" + time_s+"秒");
                } else {
                    return (time_d + "天:" + time_h + "小时:" + time_m + "分钟:" + time_s+"秒");
                }
            } else {
                return "false";
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return "false";
        }
    }
    /**
     * 时间戳转换日期
     *
     * @param time
     * @return
     */
    public static String time(String time) {
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lcc = Long.valueOf(time);

        int i = Integer.parseInt(time);

        String times = sdr.format(new Date(lcc * 1000L));
        return times;
    }
    public static String transferLongToDate(String dateFormat, String millSec) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        try {
            long l = Long.parseLong(millSec);
            Date date = new Date(l);
            return sdf.format(date);
        } catch (Exception e) {
            // TODO: handle exception
        }
        return "2016-1-1";
    }

    public static String ms2Date(long _ms){
        Date date = new Date(_ms);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        return format.format(date);
    }

    /**
     * 时间戳转换为周几
     *
     * @param timeStamp
     * @return
     */
    public static String getWeek(long timeStamp) {
        int mydate = 0;
        String week = null;
        Calendar cd = Calendar.getInstance();
        cd.setTime(new Date(timeStamp));
        mydate = cd.get(Calendar.DAY_OF_WEEK);
        // 获取指定日期转换成星期几
        if (mydate == 1) {
            week = "周日";
        } else if (mydate == 2) {
            week = "周一";
        } else if (mydate == 3) {
            week = "周二";
        } else if (mydate == 4) {
            week = "周三";
        } else if (mydate == 5) {
            week = "周四";
        } else if (mydate == 6) {
            week = "周五";
        } else if (mydate == 7) {
            week = "周六";
        }
        return week;
    }
    /**
     * 验证身份证号是否符合规则
     * @param text 身份证号
     * @return
     */
    public static boolean personIdValidation(String text) {
        String regx = "[0-9]{17}[Xx]";
        String reg1 = "[0-9]{15}";
        String regex = "[0-9]{18}";
        return text.matches(regx) || text.matches(reg1) || text.matches(regex);
    }

    /**
     * @param os
     * @param delimiter
     * @return
     * 把String集合，变成以 delimiter分割的字符串
     */
    public static String arrayToStr(List<String> os, String delimiter) {
        if (os == null || os.size() == 0) {
            return "";
        }
        StringBuilder sb = new StringBuilder(os.get(0));
        for(int i = 1; i< os.size(); i++){
            sb.append(delimiter);
            sb.append(os.get(i));
        }
        return sb.toString();
    }
    /**
     * 校验银行卡卡号
     *
     * @param cardId
     * @return
     */
    public static boolean checkBankCard(String cardId) {
        char bit = getBankCardCheckCode(cardId
                .substring(0, cardId.length() - 1));
        if (bit == 'N') {
            return false;
        }
        return cardId.charAt(cardId.length() - 1) == bit;
    }
    /**
     * 从不含校验位的银行卡卡号采用 Luhm 校验算法获得校验位
     *
     * @param nonCheckCodeCardId
     * @return
     */
    public static char getBankCardCheckCode(String nonCheckCodeCardId) {
        if (nonCheckCodeCardId == null
                || nonCheckCodeCardId.trim().length() == 0
                || !nonCheckCodeCardId.matches("\\d+")) {
            // 如果传的不是数据返回N
            return 'N';
        }
        char[] chs = nonCheckCodeCardId.trim().toCharArray();
        int luhmSum = 0;
        for (int i = chs.length - 1, j = 0; i >= 0; i--, j++) {
            int k = chs[i] - '0';
            if (j % 2 == 0) {
                k *= 2;
                k = k / 10 + k % 10;
            }
            luhmSum += k;
        }
        return (luhmSum % 10 == 0) ? '0' : (char) ((10 - luhmSum % 10) + '0');
    }

    /**
     * @param num
     * @return
     *
     */
    public static String doubleTrans(float num){
        if(num==(int)num){
            return String.valueOf((int)num);
        }
        return String.valueOf((float)(Math.round(num*100))/100);
    }
}
